package com.wd.tw.service;

import com.wd.tw.domain.entity.ElevatorCameraBox;
import com.wd.tw.domain.entity.ElevatorCameraSignal;
import com.wd.tw.domain.entity.ElevatorInfo;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @className: ElevatorConfigService
 * @description: TODO 类描述
 * @author: liumingkun
 * @date: 2025/1/8
 **/
@Service
public class ElevatorConfigService {

    @Resource
    ElevatorInfoService elevatorInfoService;

    @Resource
    ElevatorCameraBoxService cameraBoxService;

    @Resource
    ElevatorCameraSignalService cameraSignalService;

    //相机与盒子的channelId映射表
    private List<ElevatorCameraBox> cameraBoxList;

    //相机与电梯信号映射表
    private List<ElevatorCameraSignal> cameraSignalList;

    //电梯信息表
    private List<ElevatorInfo> elevatorInfoList;

    public List<ElevatorCameraBox> getCameraBoxList() {
        return cameraBoxList;
    }

    public void setCameraBoxList(List<ElevatorCameraBox> cameraBoxList) {
        this.cameraBoxList = cameraBoxList;
    }

    public List<ElevatorCameraSignal> getCameraSignalList() {
        return cameraSignalList;
    }

    public void setCameraSignalList(List<ElevatorCameraSignal> cameraSignalList) {
        this.cameraSignalList = cameraSignalList;
    }

    public List<ElevatorInfo> getElevatorInfoList() {
        return elevatorInfoList;
    }

    public void setElevatorInfoList(List<ElevatorInfo> elevatorInfoList) {
        this.elevatorInfoList = elevatorInfoList;
    }

    @PostConstruct
    public void init() {
        this.refreshConfig();
    }

    public void refreshConfig(){
        this.cameraBoxList = cameraBoxService.selectElevatorCameraBoxList(new ElevatorCameraBox());
        this.cameraSignalList =cameraSignalService.selectEnableSignalList(new ElevatorCameraSignal());
        this.elevatorInfoList=elevatorInfoService.selectElevatorInfoList(new ElevatorInfo());
    }

    public boolean getSignalState(){
        return elevatorInfoList.get(0).getSignalState();
    }

    public Long getCameraId(String channelId, String boxId) {
        ElevatorCameraBox elevatorCameraBox = cameraBoxList.stream().filter(item -> item.getBoxId().equals(boxId) && item.getChannelId().equals(channelId)).findFirst().orElse(null);
        if (elevatorCameraBox != null) {
            return elevatorCameraBox.getCameraId();
        }
        return null;
    }

    public List<Integer> getSignal(Long cameraId){
        List<Integer> signalList = cameraSignalList.stream()
                .filter(signal -> signal.getCameraId().longValue()==cameraId.longValue() )
                .map(signal->signal.getSignalId().intValue())
                .collect(Collectors.toList());
        return signalList;
    }

    public List<ElevatorCameraSignal> getSignalInfo(Long cameraId){
        List<ElevatorCameraSignal> signalList = cameraSignalList.stream()
                .filter(signal -> signal.getCameraId().longValue()==cameraId.longValue())
                .collect(Collectors.toList());
        return signalList;
    }

    public ElevatorCameraSignal getSignalInfoBySignalId(Integer signalId){
        ElevatorCameraSignal signalList = cameraSignalList.stream()
                .filter(signal -> signal.getSignalId().intValue()==signalId.intValue()).findFirst().orElse(null);
        return signalList;
    }


    //获取电梯状态的信号码，用于统计电梯运行次数和时间
    public List<Integer> getElevatorSignal(){
        List<Integer> signalList = elevatorInfoList.stream()
                .map(signal->signal.getSignalId().intValue())
                .collect(Collectors.toList());
        return signalList;
    }
}
